package br.com.ipcalc;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Ipv4Activity extends Activity {

	int position = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ipv4layout);

		final Button calcularButton = (Button) findViewById(R.id.ok);
		final EditText ipEditText = (EditText) findViewById(R.id.entry);
		final TextView resultTextView = (TextView) findViewById(R.id.label3);
		final TextView resultTextView2 = (TextView) findViewById(R.id.label4);

		final Spinner maskSpinner = (Spinner) findViewById(R.id.spinner);
		ArrayAdapter<CharSequence> maskAdapter = ArrayAdapter
				.createFromResource(this, R.array.mask_array,
						android.R.layout.simple_spinner_item);
		maskAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		maskSpinner.setAdapter(maskAdapter);

		final Spinner maskBitsSpinner = (Spinner) findViewById(R.id.spinner1);
		ArrayAdapter<CharSequence> maskBitsAdapter = ArrayAdapter
				.createFromResource(this, R.array.mask_bits_array,
						android.R.layout.simple_spinner_item);
		maskBitsAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		maskBitsSpinner.setAdapter(maskBitsAdapter);

		maskSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View v, int pos,
					long id) {
				maskBitsSpinner.setSelection(pos);
				position = pos;
			}

			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		maskBitsSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View v, int pos,
					long id) {
				maskSpinner.setSelection(pos);
				position = pos;
			}

			public void onNothingSelected(AdapterView<?> parent) {

			}
		});

		calcularButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				String ip = ipEditText.getText().toString();
				String mask = maskSpinner.getItemAtPosition(position)
						.toString();

				ValidarIp vl = new ValidarIp();

				if (vl.validar(ip)) {

					Ipv4Calc ipcalc = new Ipv4Calc(ip, mask);
					resultTextView.setText(ipcalc.getResult());
					resultTextView2.setText(ipcalc.getResultBin());


				} else {

					Context context = getApplicationContext();
					CharSequence text = "Favor informar um IP no formato\n" +"    [0-255].[0-255].[0-255].[0-255]";
					int duration = Toast.LENGTH_LONG;

					Toast toast = Toast.makeText(context, text, duration);
					toast.show();
				}

			}
		});

	}

}
